﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sc.Domain;
using System.Data.SqlClient;
using System.Data;

namespace Sc.Persistence
{
    public class Car_AccidentDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Car_Accident model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Car_Accident(");
            strSql.Append("CarId,DriverId,IgTime,IgAddress,IgRemark,IgDuty,IgOpinion,IgMoney,IsPay,PayTime,HasAttachments,Remark,InstanceState,ApproalUsres,NodeCount,UserId,IgGrade,PayMoney,InsuranceMoney,ResUnit,ResUser,ResDivision,AccAddress)");
            strSql.Append(" values (");
            strSql.Append("@CarId,@DriverId,@IgTime,@IgAddress,@IgRemark,@IgDuty,@IgOpinion,@IgMoney,@IsPay,@PayTime,@HasAttachments,@Remark,@InstanceState,@ApproalUsres,@NodeCount,@UserId,@IgGrade,@PayMoney,@InsuranceMoney,@ResUnit,@ResUser,@ResDivision,@AccAddress)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CarId", SqlDbType.Int,4),
					new SqlParameter("@DriverId", SqlDbType.Int,4),
					new SqlParameter("@IgTime", SqlDbType.DateTime),
					new SqlParameter("@IgAddress", SqlDbType.NVarChar,150),
					new SqlParameter("@IgRemark", SqlDbType.NVarChar,150),
					new SqlParameter("@IgDuty", SqlDbType.NVarChar,50),
					new SqlParameter("@IgOpinion", SqlDbType.Text),
					new SqlParameter("@IgMoney", SqlDbType.Decimal,9),
					new SqlParameter("@IsPay", SqlDbType.Bit,1),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@HasAttachments", SqlDbType.Bit,1),
					new SqlParameter("@Remark", SqlDbType.Text),
					new SqlParameter("@InstanceState", SqlDbType.Int,4),
					new SqlParameter("@ApproalUsres", SqlDbType.VarChar,100),
					new SqlParameter("@NodeCount", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@IgGrade", SqlDbType.Int,4),
					new SqlParameter("@PayMoney", SqlDbType.Decimal,9),
					new SqlParameter("@InsuranceMoney", SqlDbType.Decimal,9),
					new SqlParameter("@ResUnit", SqlDbType.NVarChar,50),
					new SqlParameter("@ResUser", SqlDbType.NVarChar,10),
					new SqlParameter("@ResDivision", SqlDbType.NVarChar,500),
					new SqlParameter("@AccAddress", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.CarId;
            parameters[1].Value = model.DriverId;
            parameters[2].Value = model.IgTime;
            parameters[3].Value = model.IgAddress;
            parameters[4].Value = model.IgRemark;
            parameters[5].Value = model.IgDuty;
            parameters[6].Value = model.IgOpinion;
            parameters[7].Value = model.IgMoney;
            parameters[8].Value = model.IsPay;
            parameters[9].Value = model.PayTime;
            parameters[10].Value = model.HasAttachments;
            parameters[11].Value = model.Remark;
            parameters[12].Value = model.InstanceState;
            parameters[13].Value = model.ApproalUsres;
            parameters[14].Value = model.NodeCount;
            parameters[15].Value = model.UserId;
            parameters[16].Value = model.IgGrade;
            parameters[17].Value = model.PayMoney;
            parameters[18].Value = model.InsuranceMoney;
            parameters[19].Value = model.ResUnit;
            parameters[20].Value = model.ResUser;
            parameters[21].Value = model.ResDivision;
            parameters[22].Value = model.AccAddress;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获取单个车辆事故数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <summary>
        public Car_Accident GetDetail(int id)
        {
            Car_Accident result = null;
            string sql = "SELECT * FROM Car_Accident WHERE Id=@Id";

            SqlParameter parm = new SqlParameter("@Id", SqlDbType.Int, 4);
            parm.Value = id;

            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parm))
            {
                if (sdr.Read())
                {
                    result = new Car_Accident();
                    result = BuildObjectFromIDataReader(sdr);
                }
                sdr.Close();
            }
            return result;
        }
        /// <summary>
        /// 从IDataReader中读取数据，组装成实体
        /// </summary>
        /// <param name="dr">数据读取接口</param>
        /// <returns>Car_Accident实体</returns>
        /// <remarks>
        /// </remarks>
        private Car_Accident BuildObjectFromIDataReader(IDataReader dr)
        {
            try
            {
                Car_Accident obj = new Car_Accident();
                obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                obj.CarId = (dr["CarId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("CarId")));
                obj.DriverId = (dr["DriverId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("DriverId")));
                obj.IgTime = (dr["IgTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("IgTime")));
                obj.IgAddress = (dr["IgAddress"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("IgAddress")));
                obj.IgRemark = (dr["IgRemark"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("IgRemark")));
                obj.IgDuty = (dr["IgDuty"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("IgDuty")));
                obj.IgOpinion = (dr["IgOpinion"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("IgOpinion")));
                obj.IgMoney = (dr["IgMoney"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("IgMoney")));
                obj.IsPay = (dr["IsPay"] == System.DBNull.Value ? false : dr.GetBoolean(dr.GetOrdinal("IsPay")));
                obj.PayTime = (dr["PayTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("PayTime")));
                obj.HasAttachments = (dr["HasAttachments"] == System.DBNull.Value ? false : dr.GetBoolean(dr.GetOrdinal("HasAttachments")));
                obj.Remark = (dr["Remark"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("Remark")));
                obj.InstanceState = (dr["InstanceState"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("InstanceState")));
                obj.ApproalUsres = (dr["ApproalUsres"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ApproalUsres")));
                obj.NodeCount = (dr["NodeCount"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("NodeCount")));
                obj.UserId = (dr["UserId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("UserId")));
                obj.IgGrade = (dr["IgGrade"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("IgGrade")));
                obj.PayMoney = (dr["PayMoney"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("PayMoney")));
                obj.InsuranceMoney = (dr["InsuranceMoney"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("InsuranceMoney")));
                obj.ResUnit = (dr["ResUnit"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ResUnit")));
                obj.ResUser = (dr["ResUser"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ResUser")));
                obj.ResDivision = (dr["ResDivision"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ResDivision")));
                obj.AccAddress = (dr["AccAddress"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("AccAddress")));
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
            }
        }
        /// <summary>
        /// 更新车辆事故申请
        /// </summary>
        /// <param name="ent">业务数据</param>
        /// <returns></returns>
        public int Update(Car_Accident ent)
        {
            string sql = "UPDATE Car_Accident SET InstanceState=@InstanceState,ApproalUsres=@ApproalUsres,NodeCount=@NodeCount WHERE Id=@Id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@InstanceState",SqlDbType.Int),
                new SqlParameter("@ApproalUsres",SqlDbType.NVarChar),
                new SqlParameter("@NodeCount",SqlDbType.Int),
                new SqlParameter("@Id",SqlDbType.Int)
            };
            parameters[0].Value = ent.InstanceState;
            parameters[1].Value = ent.ApproalUsres;
            parameters[2].Value = ent.NodeCount;
            parameters[3].Value = ent.Id;
            return DbHelperSQL.ExecuteSql(sql, parameters);
        }
        #endregion  BasicMethod
    }
}
